1. /* slfcombl.cpp by K.Tsuru */
  2. // function ID 4100 DRADIX
  3. /*************************************************
  4. SLong class
  5. It provides a binomial coefficient(combination number) nCk.
  6. n < k : error
  7. **************************************************/
  8. #ifndef SN_H
  9. #include "sn.h"
  10. #endif
  11. /***********************************************************************
  12. It provides a main body for binomial coefficient calculated by a formula
  13. n n-1 n-2 n-3 n-k+1
  14. nCk = --- x --- x --- x --- x ... x -----
  15. 1 2 3 4 k
  16. It is used the fact that any partial product is an integer.
  17. *************************************************************************/
  18. SLong combL(ulong n, ulong k){
  19. if( n < k ) SNManager::SetError(SNManager::DOMAIN_ERR,"combL()", 4100);
  20. if( n - k < k ) k = n - k;
  21. if( k <= 1u ) return k ? n : 1;
  22. #if 1
  23. SInteger nCk(n);
  24. ulong mt = nCk.SlOpMaxValue(); // =131071
  25. //It uses SInteger IsMult() and IsDiv().
  26. if(n < mt){ // k < n < mt
  27. ulong j;
  28. for(j = 1; j < k ; j++){
  29. IsMult(nCk, n - j, nCk);
  30. IsDiv(nCk, j + 1, nCk);
  31. }
  32. return nCk.ConvToDec(); //radix conversion
  33. } else { // uses SLong
  34. SLong r(n);
  35. ulong j;
  36. for(j = 1; j < k ; j++){ r= LsMult(r,n - j); r = LsDiv(r, j + 1); }
  37. return r;
  38. }
  39. #else
  40. // SLong version
  41. SLong r(n);
  42. ulong j;
  43. for(j = 1; j < k ; j++){ r= LsMult(r,n - j); r = LsDiv(r, j + 1); }
  44. return r;
  45. #endif
  46. }

slfcombl.cpp : last modifiled at 2016/10/19 20:14:17(1,454 bytes)
created at 2017/10/07 10:26:50
The creation time of this html file is 2017/11/09 14:52:03 (Thu Nov 09 14:52:03 2017).